package cn.ren.service;

import cn.ren.constant.MessageConstant;
import cn.ren.dao.MemberDao;
import cn.ren.dao.OrderDao;
import cn.ren.dao.OrderSettingDao;
import cn.ren.dao.SetMealDao;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.entity.Result;
import cn.ren.pojo.*;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @program: itcast_health
 * @description:
 * @author: ren
 * @create: 2020-03-04 22:22
 **/
@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetMealDao setMealDao;

    @Override
    public List<Integer> findMemberCountByMonth(List<String> list) {
        //根据月份list，生成月底日期,然后查询该日期前所有的数量
        List<Integer> arrayList = new ArrayList<>();
        for (String s : list) {
            Integer count = memberDao.findMemberCountBeforeDate(s + ".31");
            arrayList.add(count);
        }
        return arrayList;
    }

    @Override
    public Result dj_add(Member member, Integer[] setmealIds) {
        //获取用户手机号
        String phoneNumber = member.getPhoneNumber();
        //判断用户是否存在
        Member memberByTel = memberDao.findMemberByTel(phoneNumber);
        if(memberByTel==null){
            //用户不存在，注册用户
            memberDao.add(member);
        }else {
            //用户存在的情况
            member.setId(memberByTel.getId());
        }

        //获取用户预约时间
        Date regTime = member.getRegTime();

        //1.判断这一天系统是否允许预约
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(regTime);
        if (orderSetting == null){
            //这一天没有预约计划
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2.判断这一天是否已经预约满了
        int number = orderSetting.getNumber();      //这天可以预约的最大数量
        int reservations = orderSetting.getReservations();  //这天的已预约人数
        if (reservations >= number){
            //这一天预约已满
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //3.判断所选套餐是否预约
        String setmealNames="";
        boolean flag=false;
        for (Integer setmealId : setmealIds) {
            Map<String,Object> map=new HashMap<>();
            map.put("setmealId",setmealId);
            map.put("orderDate",regTime);
            map.put("memberid",member.getId());
            long count=orderDao.dj_findBySetmealIdAndOrderDate(map);
            if(count>0){
                //套餐已经预约
                //记录已经预约套餐的名称
                Setmeal setmeal = setMealDao.findById(setmealId);
                setmealNames+=setmeal.getName()+",";
                continue;
            }else {
                flag=true;
                //套餐没有预约，现在开始预约套餐
                Order order=new Order();
                order.setMemberId(member.getId());
                order.setOrderDate(member.getRegTime());
                order.setOrderType(Order.ORDERTYPE_TELEPHONE);
                order.setOrderStatus(Order.ORDERSTATUS_NO);
                order.setSetmealId(setmealId);
                orderDao.add(order);
                //预约成功，预约人数+1
                orderSetting.setReservations(reservations + 1);
                orderSettingDao.editReservationsByOrderDate(orderSetting);
            }
        }
        if(flag){
            if(setmealNames.length()>0){
                return new Result(true,setmealNames+"已经预约过了,其余的预约成功！");
            }else {
                return new Result(true,"预约成功！");
            }
        }else {
            return new Result(true,setmealNames+"已经预约过了...");
        }
    }

    @Override
    public List<Member> findAll() {

        return memberDao.findAll();
    }

    @Override
    public PageResult queryPage(Integer currentPage, Integer pageSize, String queryString) {


        return null;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Member> page = memberDao.queryByCondition(queryPageBean.getQueryString());
        System.out.println(page.getResult());
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public List<Illness> findAllIllness() {
        return memberDao.findAllIllness();
    }

    @Override
    public PageResult findIllnessPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Member> page = memberDao.queryByIllnessPage(queryPageBean.getQueryString());
        List<Member> result = page.getResult();
        for (Member member : result) {
            System.out.println(member);
        }
        System.out.println("result"+result);
        return new PageResult(page.getTotal(),page.getResult());
    }
}
